package com.seesmic.helper
{
	/*
	Copyright 2007 SEESMIC, Inc
	Licensed under the Apache License, Version 2.0 (the "License"); 
	you may not use this file except in compliance with the License. You may obtain a copy of the License at 
	
	http://www.apache.org/licenses/LICENSE-2.0 
	
	Unless required by applicable law or agreed to in writing, 
	software distributed under the License is distributed on an 
	"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
	See the License for the specific language governing permissions and limitations under the License.
	*/
	
	import flash.events.Event;
	import flash.events.TimerEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.utils.Dictionary;
	import flash.utils.Timer;

	[Event(name="timeout", type="flash.events.Event")]
	public class URLLoaderBase extends URLLoader
	{
		
		public static const TIME_OUT:String  = "timeout";		
		private var listeners:Dictionary = new Dictionary();
		
		/**
		* Timeout
		 * */
		public var timeout:Number = 20*1000;
		private var timer:Timer;
		
		
		public function URLLoaderBase(request:URLRequest=null)
		{
		 
			super(request);
			
			 
		}
		
		public function removeAllListener():void
		{
		
			for(var i:* in listeners)
			{
				this.removeEventListener(i, listeners[i]);
			}
			timer.removeEventListener(TimerEvent.TIMER, onTimer);
			timer.stop()
		}
		public override function addEventListener(type:String, listener:Function, useCapture:Boolean=false, priority:int=0, useWeakReference:Boolean=false):void
		{
			super.addEventListener(type, listener, useCapture, priority, useWeakReference);
			listeners[type] = listener
		}
		
		
		public override function load(request:URLRequest):void
		{
			super.load(request)
			timer = new Timer(timeout, 1)
			timer.addEventListener(TimerEvent.TIMER, onTimer);
			timer.start()
		}
		
		private function onTimer(e:TimerEvent):void
		{
			this.dispatchEvent(new Event(TIME_OUT, true))
			removeAllListener()
		}
	}
}